package com.controller.search 
{	
	import com.model.valueObjects.VoCancelledPo;
	import com.service.POSearch;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class POSearchController extends AbstractController
	{
		
		[Inject]
		public var poSearch:POSearch;
		
		public function POSearchController()
		{
			super();
		}
		
		[EventHandler(event="POSearchEvent.CANCEL_PO",properties="cancelPoVo")]
		public function cancelPo(cancelPoVo:VoCancelledPo):void{
			sh.executeServiceCall(poSearch.cancelPoItem(cancelPoVo),cancelPoResult, handleFault );
		}
		private function cancelPoResult(event:ResultEvent):void
		{
			getRecentPo();
			Alert.show(event.result as String,'Info');
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_CANCELLED_PO_HEADER")]
		public function getCancelledPoHeader():void{
			sh.executeServiceCall(poSearch.getCancelledPos(),getCancelledPoHeaderResult, handleFault );
		}		
		private function getCancelledPoHeaderResult(event:ResultEvent):void
		{
			model.poSearchHeader = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_CANCELLED_PO_DETAILS",properties="po_serialnumber")]
		public function getCancelledPoDetails(po_serialnumber:int):void{
			sh.executeServiceCall(poSearch.getCancelledPoDetails(po_serialnumber),getCancelledPoDetailsResult, handleFault );
		}		
		private function getCancelledPoDetailsResult(event:ResultEvent):void
		{
			model.poSearchDetail = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_RECENT_PO_HEADER")]
		public function getRecentPo():void{
			sh.executeServiceCall(poSearch.getRecentPOs(),getRecentPoResult, handleFault );
		}		
		private function getRecentPoResult(event:ResultEvent):void
		{
			model.poSearchHeader = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_PO_DETAILS",properties="po_serialnumber")]
		public function getPoDetails(po_serialnumber:Number):void{
			sh.executeServiceCall(poSearch.getPoDetails(po_serialnumber),getPoDetailsResult, handleFault );
		}		
		private function getPoDetailsResult(event:ResultEvent):void
		{
			model.poSearchDetail = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_COMPLETED_PO_HEADER")]
		public function getCompletedPoHeader():void{
			sh.executeServiceCall(poSearch.getCompletedPOs(),getCompletedPoHeaderResult, handleFault );
		}		
		private function getCompletedPoHeaderResult(event:ResultEvent):void
		{
			model.poSearchHeader = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_COMPLETED_PO_DETAILS",properties="po_serialnumber")]
		public function getCompletedPoDetails(po_serialnumber:Number):void{
			sh.executeServiceCall(poSearch.getCompletedPoDetails(po_serialnumber),getCompletedPoDetailsResult, handleFault );
		}		
		private function getCompletedPoDetailsResult(event:ResultEvent):void
		{
			model.poSearchDetail = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_CONFORM_PO_HEADER")]
		public function getConformPoHeader():void{
			sh.executeServiceCall(poSearch.getConformedPOs(),getConformPoHeaderResult, handleFault );
		}		
		private function getConformPoHeaderResult(event:ResultEvent):void
		{
			model.poSearchHeader = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_CONFORM_PO_DETAILS",properties="po_serialnumber")]
		public function getOutConformPoDetails(po_serialnumber:Number):void{
			sh.executeServiceCall(poSearch.getConformedPoDetails(po_serialnumber),getConformPoDetailsResult, handleFault );
		}		
		private function getConformPoDetailsResult(event:ResultEvent):void
		{
			model.poSearchDetail = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_OUTSTANDING_PO_HEADER")]
		public function getOutstandingPoHeader():void{
			sh.executeServiceCall(poSearch.getOutStandingPOs(),getOutstandingPoHeaderResult, handleFault );
		}		
		private function getOutstandingPoHeaderResult(event:ResultEvent):void
		{
			model.poSearchHeader = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_OUTSTANDING_PO_DETAILS",properties="po_serialnumber")]
		public function getOutstandingPoDetails(po_serialnumber:Number):void{
			sh.executeServiceCall(poSearch.getOutstandingPoDetails(po_serialnumber),getOutstandingPoDetailsResult, handleFault );
		}		
		private function getOutstandingPoDetailsResult(event:ResultEvent):void
		{
			model.poSearchDetail = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="POSearchEvent.GET_GR_LINKS",properties="po_detserialnumber_fk")]
		public function getGrLinks(po_detserialnumber_fk:Number):void{
			sh.executeServiceCall(poSearch.getGrLinks(po_detserialnumber_fk),getGrLinksResult, handleFault );
		}		
		private function getGrLinksResult(event:ResultEvent):void
		{
			model.poSearchGrLinks = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}